﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Business.AID;
using Data.AID;
using Microsoft.SharePoint;

namespace Presentation.AID.WebPart.Presentation.AID.VWPAuditoriaValidacaoRelatorio
{
    public partial class VWPAuditoriaValidacaoRelatorioUserControl : UserControl
    {
        protected void Page_PreRender(object sender, EventArgs e)
        {
            ColaboradorBO colaboradorBO = new ColaboradorBO();
            Colaborador colaborador = colaboradorBO.GetEmployeeByLogin(SPContext.Current.Web.CurrentUser.LoginName);

            if (colaborador == null || !colaborador.Funcao.Contains("Coordenador de Auditoria"))
            {
                gdvRelatorioFormulario.DataSource = null;
                gdvRelatorioFormulario.DataBind();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack) 
            {
                RecuperaFormulariosPendentes();
            }
        }

        public void RecuperaFormulariosPendentes() 
        {
            FormularioBO formularioBO = new FormularioBO();
            gdvRelatorioFormulario.DataSource = formularioBO.GetFormsByStage(5);
            gdvRelatorioFormulario.DataBind();
        }
        protected void gdvRelatorioFormulario_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                string commandName = e.CommandName;
                int rowIndex = Convert.ToInt32(e.CommandArgument); 
                
                DataKey dtKey = gdvRelatorioFormulario.DataKeys[rowIndex];
                int formID = Convert.ToInt32(dtKey["ID"]);

                if (commandName == "Visualizar")
                {
                    Response.Redirect(@"/SitePages/AprovarMeta/AuditoriaValidacao.aspx?idform=" + formID);
                }

            }
            catch (Exception ex) 
            {
                throw new Exception("Erro ao redirecionar o usuário a página de validação." + ex.Message);
            }
        }
    }
}
